const { Model } = require('sequelize');

class Product extends Model {
    // 实例方法：检查是否有库存
    hasStock(quantity = 1) {
        return this.stock >= quantity;
    }

    // 实例方法：检查是否库存不足
    isLowStock() {
        return this.stock <= this.lowStockThreshold;
    }

    // 实例方法：获取折扣百分比
    getDiscountPercentage() {
        if (this.originalPrice && this.originalPrice > this.price) {
            return Math.round(((this.originalPrice - this.price) / this.originalPrice) * 100);
        }
        return 0;
    }
}

// 初始化模型，确保接收DataTypes参数
const initProduct = (sequelize, DataTypes) => {
    Product.init({
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        name: {
            type: DataTypes.STRING(200),
            allowNull: false,
            validate: {
                notEmpty: {
                    msg: '商品名称不能为空'
                },
                len: {
                    args: [2, 200],
                    msg: '商品名称长度必须在2-200个字符之间'
                }
            }
        },
        description: {
            type: DataTypes.TEXT,
            allowNull: true
        },
        shortDescription: {
            type: DataTypes.STRING(500),
            allowNull: true,
            validate: {
                len: {
                    args: [0, 500],
                    msg: '简短描述长度不能超过500个字符'
                }
            }
        },
        sku: {
            type: DataTypes.STRING(100),
            allowNull: false,
            unique: true,
            validate: {
                notEmpty: {
                    msg: 'SKU不能为空'
                },
                len: {
                    args: [1, 100],
                    msg: 'SKU长度必须在1-100个字符之间'
                }
            }
        },
        price: {
            type: DataTypes.DECIMAL(10, 2),
            allowNull: false,
            validate: {
                min: {
                    args: [0],
                    msg: '价格不能为负数'
                }
            }
        },
        originalPrice: {
            type: DataTypes.DECIMAL(10, 2),
            allowNull: true,
            validate: {
                min: {
                    args: [0],
                    msg: '原价不能为负数'
                }
            }
        },
        stock: {
            type: DataTypes.INTEGER,
            defaultValue: 0,
            validate: {
                min: {
                    args: [0],
                    msg: '库存不能为负数'
                }
            }
        },
        lowStockThreshold: {
            type: DataTypes.INTEGER,
            defaultValue: 10,
            validate: {
                min: {
                    args: [0],
                    msg: '低库存阈值不能为负数'
                }
            }
        },
        weight: {
            type: DataTypes.DECIMAL(8, 2),
            allowNull: true,
            validate: {
                min: {
                    args: [0],
                    msg: '重量不能为负数'
                }
            }
        },
        dimensions: {
            type: DataTypes.STRING(100),
            allowNull: true
        },
        images: {
            type: DataTypes.JSON,
            allowNull: true,
            defaultValue: []
        },
        tags: {
            type: DataTypes.JSON,
            allowNull: true,
            defaultValue: []
        },
        isActive: {
            type: DataTypes.BOOLEAN,
            defaultValue: true
        },
        isFeatured: {
            type: DataTypes.BOOLEAN,
            defaultValue: false
        },
        viewCount: {
            type: DataTypes.INTEGER,
            defaultValue: 0
        },
        salesCount: {
            type: DataTypes.INTEGER,
            defaultValue: 0
        },
        categoryId: {
            type: DataTypes.INTEGER,
            allowNull: false,
            references: {
                model: 'categories',
                key: 'id'
            }
        }
    }, {
        sequelize,
        modelName: 'Product',
        tableName: 'products',
        timestamps: true,
        indexes: [
            { fields: ['categoryId'] },
            { fields: ['sku'] },
            { fields: ['isActive'] },
            { fields: ['isFeatured'] }
        ]
    });

    return Product;
};

module.exports = initProduct;